package com.fan.shop.views.bean;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ser.FilterProvider;
import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;

import com.fan.shop.businesslogic.StoreCreateProcess;
import com.fan.shop.businesslogic.StoreDeleteProcess;
import com.fan.shop.businesslogic.StoreUpdateProcess;
import com.fan.shop.global.JacksonMapper;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.services.StoreService;

@ManagedBean(name = "storesBean")
@RequestScoped
public class StoresBean {

	private static Logger logger = Logger.getLogger(StoresBean.class);

	@ManagedProperty(value = "#{storeService}")
	private StoreService storeService;

	private String tableData;
	private int columnsSize;
	private List<String> columnNames;
	private String editStoreCode;
	private String editStoreName;
	private String editAddress;
	private int editSuspended;

	private String selectedValue;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		StoresBean.logger = logger;
	}

	public StoreService getStoreService() {
		return storeService;
	}

	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}

	public String getTableData() {
		ObjectMapper mapper = JacksonMapper.getInstance();

		Set<String> columnNames = new HashSet<String>();
		for (String name : storeService.getColumnNames()) {
			columnNames.add(name);

		}
		FilterProvider filters = new SimpleFilterProvider().addFilter(
				"myFilter",
				SimpleBeanPropertyFilter.filterOutAllExcept(columnNames));

		tableData = "\"aaData\": [";
		StringBuffer data = new StringBuffer();

		for (Object obj : storeService.getList()) {
			try {
				String line = mapper.writer(filters).writeValueAsString(obj);
				data = data.append(line);

			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String tmp = data.toString().replaceAll("\\{[^,:\\{\\}]*:", "[");
		tmp = tmp.replaceAll(",[^,:\\{\\}]*:", ",");

		tableData = tableData + tmp.replace("{", "[").replace("}", "],");
		if (tableData.endsWith(",")) {
			tableData = tableData.substring(0, tableData.length() - 1);
		}

		tableData = tableData + "]";

		return tableData;
	}

	public void setTableData(String tableData) {
		this.tableData = tableData;
	}

	public int getColumnsSize() {
		columnsSize = getColumnNames().size();
		return columnsSize;
	}

	public void setColumnsSize(int columnsSize) {
		this.columnsSize = columnsSize;
	}

	public List<String> getColumnNames() {
		columnNames = storeService.getColumnNames();
		return columnNames;
	}

	public void setColumnNames(List<String> columnNames) {
		this.columnNames = columnNames;
	}

	public String getEditStoreCode() {
		return editStoreCode;
	}

	public void setEditStoreCode(String editStoreCode) {
		this.editStoreCode = editStoreCode;
	}

	public String getEditStoreName() {
		return editStoreName;
	}

	public void setEditStoreName(String editStoreName) {
		this.editStoreName = editStoreName;
	}

	public String getEditAddress() {
		return editAddress;
	}

	public void setEditAddress(String editAddress) {
		this.editAddress = editAddress;
	}

	public int getEditSuspended() {
		return editSuspended;
	}

	public void setEditSuspended(int editSuspended) {
		this.editSuspended = editSuspended;
	}

	public String getSelectedValue() {
		return selectedValue;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}

	public String createStoreAction() {
		StoreCreateProcess storeCreateProcess = new StoreCreateProcess();
		storeCreateProcess.setStoreCode(editStoreCode);
		storeCreateProcess.setStoreName(editStoreName);
		storeCreateProcess.setAddress(editAddress);
		storeCreateProcess.setSuspended(editSuspended);
		storeCreateProcess.setStoreService(storeService);

		int result = storeCreateProcess.process();
		clearFields();
		if (result == ShopGlobalVariables.FAILED) {
			return "stores.faces";
		} else {
			return "stores.faces";
		}
	}

	public String updateStoreAction() {
		StoreUpdateProcess storeUpdateProcess = new StoreUpdateProcess();
		storeUpdateProcess.setStoreCode(editStoreCode);
		storeUpdateProcess.setStoreName(editStoreName);
		storeUpdateProcess.setAddress(editAddress);
		storeUpdateProcess.setSuspended(editSuspended);
		storeUpdateProcess.setStoreService(storeService);
		int result = storeUpdateProcess.process();
		clearFields();
		return "stores.faces";
	}

	public String deleteStoreAction() {
		StoreDeleteProcess storeDeleteProcess = new StoreDeleteProcess();
		storeDeleteProcess.setStoreCode(editStoreCode);
		storeDeleteProcess.setStoreService(storeService);
		
		int result = storeDeleteProcess.process();
		clearFields();
		return "stores.faces";
	}

	public void clearFields() {
		editStoreCode = "";
		editAddress = "";
		editStoreName = "";
		editSuspended = 0;
	}
}
